package com.dynamic.test;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.Loader;
import javassist.NotFoundException;
import javassist.Translator;

public class JavassistRun {
	public static void main(String[] args) {
		if (args.length >= 1) {

			try {
				// set up class loader with translator
				Translator xlat = new VerboseTranslator();
				ClassPool pool = ClassPool.getDefault();
				Loader loader = new Loader(pool);

				// invoke "main" method of target class
				String[] pargs = new String[args.length - 1];
				System.arraycopy(args, 1, pargs, 0, pargs.length);
				loader.run(args[0], pargs);
			} catch (Throwable e) {
				e.printStackTrace();
			}

		} else {
			System.out.println("Usage: JavassistRun main-class args...");
		}
	}

	public static class VerboseTranslator implements Translator {
		public void start(ClassPool pool) {
		}

		public void onWrite(ClassPool pool, String cname) {
			System.out.println("onWrite called for " + cname);
		}

		public void onLoad(ClassPool arg0, String arg1)
				throws NotFoundException, CannotCompileException {
			// TODO Auto-generated method stub

		}
	}
}
